home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 526-550 / disk_540 / cliexe / cliexe.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  9KB  |  380 lines

  1. /*
  2.  *    CLIExe.c - Copyright © 1990 by S.R.
  3.  *:    29 Jul 1991    15:44:33
  4.  *    Created:    25 May 1991    09:58:27
  5.  *    Modified:    29 Jul 1991    15:40:10
  6.  *
  7.  * Make>> delete <file>.o
  8.  *    Make>> cc -qf -ps -wp -wd -wu -wr -so -sb -hi Aztec:Include/x16.dmp <file>.c
  9.  *    Make>> ln <file>.o -larpsc -lreq
  10.  */
  11.  
  12.  
  13. /***** Same code that ParM.library, so same Struct ******/
  14. #include "ParmBase.h"
  15.  
  16. struct ExecBase *SysBase;
  17. struct ArpBase *ArpBase;
  18. struct GfxBase *GfxBase;
  19. struct IntuitionBase *IntuitionBase;
  20. struct IconBase *IconBase;
  21. struct ReqLib *ReqBase;
  22. struct WBStartup *WBenchMsg;
  23.  
  24. char HelpUsage[] = "CLIExe V1.3 © 1991 by S.R.\nUsage: CLIExe <FULLCMDS> command1;command2;...\n\t[MODERUN] [SHELLCMD] newshell_command\n\t[WINDOW] con:a/b/c/d/title [STACK] stacksize\n\t[TMPDIR] directory [PRI] pri [NOIO]\n";
  25. char HelpArg[] = "FULLCMDS/A,MODERUN/s,SHELLCMD/k,WINDOW/k,STACK/k,TMPDIR/k,PRI/k,NOIO/s";
  26.  
  27. long DosWrite(BPTR file, char *buffer, long length);
  28. #pragma amicall(DOSBase, 0x30, DosWrite(d1,d2,d3))
  29.  
  30. extern void setmem(void *mem, size_t size, long value);
  31.  
  32. /******* Came from ParM.library.c *********/
  33. /* Execute a CLI command as background or interactive shell */
  34.  
  35. void BuiltIn_Run(struct ParMConfig * PCfg, struct RunInfo * Command, BYTE Mode)
  36. {
  37.     struct NewShell *NS;
  38.     struct Process *pp;
  39.     char *Window, *Cmd;
  40.     BPTR fh;
  41.     short i = 0, err;
  42.     char FromFile[32], CmdBuf[128];
  43.  
  44.     if (!(NS = AllocMem(sizeof(struct NewShell), MEMF_PUBLIC | MEMF_CLEAR)))
  45.         return;
  46.     pp = (struct Process *) SysBase->ThisTask;
  47.     NS->nsh_StackSize = Command->ri_Stack;
  48.     NS->nsh_Pri = Command->ri_Pri;
  49.     NS->nsh_Input = pp->pr_CIS;
  50.     NS->nsh_Output = pp->pr_COS;
  51.     NS->nsh_Control = BACKGROUND_SHELL;
  52.  
  53.     Cmd = Command->ri_Cmd;
  54.     if (Mode == TOK_SHELL)
  55.         {
  56.             for (;;)
  57.                 {
  58.                     SPrintf(FromFile, "%sParMCmd%d", PCfg->TmpDir, i++);
  59.                     fh = Open(FromFile, MODE_NEWFILE);
  60.                     if (fh)
  61.                         break;
  62.                     else if ((err = IoErr()) != ERROR_OBJECT_IN_USE || i > 32)
  63.                         {
  64.                             if (ReqBase)
  65.                                 SimpleRequest(PCfg->ReqTitle, "Unable to open script file\n");
  66.                             FreeMem(NS, sizeof(struct NewShell));
  67.                             return;
  68.                         }
  69.                 }
  70.             FPrintf(fh, "%s\nEndCLI >NIL:\n", Cmd);
  71.             Close(fh);
  72.             if (!(Window = Command->ri_Window))
  73.                 Window = PCfg->ShellWindow;
  74.             SPrintf(CmdBuf, "\"%s\" \"%s\" From %s", PCfg->ShellCmd, Window, FromFile);
  75.             Cmd = CmdBuf;
  76.         }
  77.     ASyncRun(Cmd, NULL, (struct ProcessControlBlock *) NS);
  78.     FreeMem(NS, sizeof(struct NewShell));
  79. }
  80.  
  81. /*
  82.  *    Parse a line that may contain semicolons. Backslash ('\') is the override
  83.  *    char. This function is called from ParseConfig() and from Command().
  84.  */
  85.  
  86. void BuiltIn_ParseLine(char *cmd)
  87. {
  88.     char *s, *d, c;
  89.  
  90.     s = d = cmd;
  91.     while (c = *d++ = *s++)
  92.         {
  93.             if (c == '\\')
  94.                 *(d - 1) = *s++;
  95.             else if (c == ';')
  96.                 *(d - 1) = '\n';
  97.         }
  98. }
  99.  
  100. /*****  make (and allocate) a copy of the passed string *****/
  101.  
  102. char *BuiltIn_CopyStr(char *str)
  103. {
  104.     /*struct ParMBase *ParMBase;*/
  105.     char *newstr;
  106.  
  107.     if (newstr = AllocMem(strlen(str) + 1, MEMF_PUBLIC))
  108.         strcpy(newstr, str);
  109.     return newstr;
  110. }
  111.  
  112.  
  113. void BuiltIn_FreeStr(char *str)
  114. {
  115.     /*struct ParMBase *ParMBase;*/
  116.  
  117.     if (str)
  118.         FreeMem(str, strlen(str) + 1);
  119. }
  120.  
  121. /***** End of ParM.library.c import *****/
  122.  
  123. void main(int argc, char **argv)
  124. {
  125.     struct ParMConfig ParMConfig;
  126.     struct RunInfo Command;
  127.     long Stack = 0;        /* Stack=0 mean use IconStack */
  128.     BYTE Pri = 0, i;
  129.     char Cmd[255];
  130.     struct DiskObject *dop;
  131.     char *ToolArg;
  132.     BYTE Mode = TOK_SHELL;
  133.  
  134.     APTR OldWindowPtr;
  135.     BPTR Nfh;
  136.     struct Process *MainProcess;
  137.     BOOL NoIO;
  138.  
  139.     setmem(&ParMConfig, sizeof(struct ParMConfig), 0);
  140.     setmem(&Command, sizeof(struct RunInfo), 0);
  141.  
  142.     MainProcess = (struct Process *) SysBase->ThisTask;
  143.  
  144.     ParMConfig.ReqTitle = "CliExe";
  145.     strcpy(ParMConfig.TmpDir, DEFAULT_TMP_DIR);
  146.  
  147.     if (WBenchMsg)
  148.         {        /* Tool Types parsing */
  149.             for (i = 1; i < WBenchMsg->sm_NumArgs; i++)
  150.                 {
  151.                     CurrentDir(WBenchMsg->sm_ArgList[i].wa_Lock);    /* enter in the dir containing the Icon */
  152.                     if (dop = GetDiskObject(WBenchMsg->sm_ArgList[i].wa_Name))
  153.                         {
  154.                             ParMConfig.DefaultStack = dop->do_StackSize;
  155.  
  156.                             if (ToolArg = FindToolType(dop->do_ToolTypes, "FULLCMD"))
  157.                                 {
  158.                                     strcpy(Cmd, ToolArg);
  159.                                     BuiltIn_ParseLine(Cmd);
  160.                                     Command.ri_Cmd = Cmd;
  161.                                 }
  162.                             else
  163.                                 {
  164.                                     /* Error: no Cmd !! */
  165.                                     FreeDiskObject(dop);
  166.                                     if (ReqBase)
  167.                                         SimpleRequest(ParMConfig.ReqTitle, "I can't find the CMD.");
  168.                                     return;
  169.                                 }
  170.  
  171.                             if (ToolArg = FindToolType(dop->do_ToolTypes, "MODERUN"))
  172.                                 {
  173.                                     /* MODERUN so run mode is TOK_RUN */
  174.                                     Mode = TOK_RUN;
  175.                                     if (ToolArg = FindToolType(dop->do_ToolTypes, "PRI"))
  176.                                         Command.ri_Pri = Atol(ToolArg);
  177.                                     else
  178.                                         Command.ri_Pri = 0;
  179.                                 }
  180.                             else
  181.                                 {
  182.                                     Mode = TOK_SHELL;
  183.                                     if (ToolArg = FindToolType(dop->do_ToolTypes, "SHELLCMD"))
  184.                                         strcpy(ParMConfig.ShellCmd, ToolArg);
  185.                                     else
  186.                                         strcpy(ParMConfig.ShellCmd, DEFAULT_SHELL_CMD);
  187.  
  188.                                     if (ToolArg = FindToolType(dop->do_ToolTypes, "WINDOW"))
  189.                                         Command.ri_Window = BuiltIn_CopyStr(ToolArg);
  190.                                     else
  191.                                         Command.ri_Window = BuiltIn_CopyStr(DEFAULT_SHELL_WINDOW);
  192.  
  193.                                     if (ToolArg = FindToolType(dop->do_ToolTypes, "STACK"))
  194.                                         Command.ri_Stack = Atol(ToolArg);
  195.                                     else
  196.                                         Command.ri_Stack = 4096;
  197.  
  198.                                     Command.ri_Pri = 0;    /* in SHELL mode pri can't be something else */
  199.  
  200.                                     if (ToolArg = FindToolType(dop->do_ToolTypes, "TMPDIR"))
  201.                                         strcpy(ParMConfig.TmpDir, ToolArg);
  202.                                     else
  203.                                         strcpy(ParMConfig.TmpDir, DEFAULT_TMP_DIR);
  204.                                 }
  205.                             if (ToolArg = FindToolType(dop->do_ToolTypes, "NOIO"))
  206.                                 NoIO = TRUE;
  207.                             else
  208.                                 NoIO = FALSE;
  209.  
  210.                             FreeDiskObject(dop);
  211.  
  212.                         }
  213.                     else
  214.                         {
  215.                             /* no icon: I can't do anything ! */
  216.                             if (ReqBase)
  217.                                 SimpleRequest(ParMConfig.ReqTitle, "I can't find the icon:\"%s\".", WBenchMsg->sm_ArgList[i].wa_Name);
  218.                             return;
  219.                         }
  220.                 }
  221.         }
  222.     else
  223.         {        /* CLI parsing */
  224.  
  225.             if (!argc)
  226.                 {
  227.                     Printf("%s", HelpUsage);
  228.                     return;
  229.                 }
  230.  
  231.             /* record line */
  232.             strcpy(Cmd, argv[0]);
  233.             BuiltIn_ParseLine(Cmd);
  234.             Command.ri_Cmd = Cmd;
  235.  
  236.             /* test PRI */
  237.             if (argv[6])
  238.                 Command.ri_Pri = Atol( argv[6]);
  239.             else
  240.                 Command.ri_Pri = 0;
  241.  
  242.             if (argv[7])
  243.                 NoIO = TRUE;
  244.             else
  245.                 NoIO = FALSE;
  246.  
  247.             /* test run mode */
  248.             if (argv[1])
  249.                 {
  250.                     /* MODERUN so run mode is TOK_RUN */
  251.                     Mode = TOK_RUN;
  252.                 }
  253.             else
  254.                 {
  255.                     Mode = TOK_SHELL;
  256.  
  257.                     if (argv[2])
  258.                         strcpy(ParMConfig.ShellCmd, argv[2]);
  259.                     else
  260.                         strcpy(ParMConfig.ShellCmd, DEFAULT_SHELL_CMD);
  261.  
  262.                     if (argv[3])
  263.                         Command.ri_Window = BuiltIn_CopyStr(argv[3]);
  264.                     else
  265.                         Command.ri_Window = BuiltIn_CopyStr(DEFAULT_SHELL_WINDOW);
  266.  
  267.                     if (argv[4])
  268.                         Command.ri_Stack = Atol(argv[4]);
  269.                     else
  270.                         Command.ri_Stack = 4096;
  271.  
  272.                     if (argv[5])
  273.                         strcpy(ParMConfig.TmpDir, argv[5]);
  274.                     else
  275.                         strcpy(ParMConfig.TmpDir, DEFAULT_TMP_DIR);
  276.  
  277.                         }
  278.  
  279.  
  280.         }
  281.  
  282.     if ( NoIO)
  283.         {
  284.             /* this is for redirection problem in RUN mode */
  285.             OldWindowPtr = MainProcess->pr_WindowPtr;
  286.             MainProcess->pr_WindowPtr = (APTR) - 1;    /* Prevent request if NULL: is not mounted */
  287.             if ((Nfh = Open("NULL:", MODE_NEWFILE)) || (Nfh = Open("NIL:", MODE_NEWFILE)))
  288.                 {
  289.                     MainProcess->pr_CIS = Nfh;
  290.                     MainProcess->pr_COS = Nfh;
  291.                     MainProcess->pr_ConsoleTask = (APTR) ((struct FileHandle *) (Nfh << 2))->fh_Type;
  292.                 }
  293.             MainProcess->pr_WindowPtr = OldWindowPtr;
  294.         }
  295.  
  296.     BuiltIn_Run(&ParMConfig, &Command, Mode);
  297.     BuiltIn_FreeStr(Command.ri_Window);
  298.  
  299. }
  300.  
  301.  
  302. /**** Start *********************************/
  303. void exit(int code)
  304. {
  305.     if (WBenchMsg)
  306.         {
  307.             Forbid();
  308.             ReplyMsg((struct Message *) WBenchMsg);
  309.         }
  310.     CloseLibrary(ArpBase);
  311.     Exit(code);
  312. }
  313.  
  314.  
  315. void _main(long alen, char *aptr)
  316. {
  317.     struct DosLibrary *DOSBase;
  318.     register struct Process *pp;
  319.     int _argc;
  320.     char **_argv;
  321.  
  322.     pp = (struct Process *) SysBase->ThisTask;
  323.     if (!pp->pr_CLI)
  324.         {
  325.             WaitPort(&pp->pr_MsgPort);
  326.             WBenchMsg = (struct WBStartup *) GetMsg(&pp->pr_MsgPort);
  327.         }
  328.     if (!(ArpBase = (struct ArpBase *) OpenLibrary("arp.library", 39L)))
  329.         {
  330.             if (pp->pr_CLI && (DOSBase = OpenLibrary("dos.library", 0L)))
  331.                 {
  332.                     DosWrite(pp->pr_COS, "You need arp.library V39+\n", 26L);
  333.                     DosWrite(pp->pr_COS, "You need Req.library V1.22+\n", 27L);
  334.                     CloseLibrary(DOSBase);
  335.                 }
  336.             return;
  337.         }
  338.     DOSBase = (struct DosLibrary *) ArpBase->DosBase;
  339.     IntuitionBase = (struct IntuitionBase *) ArpBase->IntuiBase;
  340.     GfxBase = (struct GfxBase *) ArpBase->GfxBase;
  341.  
  342.     ReqBase = (struct ReqLib *) ArpOpenLibrary("req.library", 1L);
  343.     IconBase = (struct IconBase *) ArpOpenLibrary("icon.library", 1L);
  344.     if (!(IconBase = (struct IconBase *) ArpOpenLibrary("icon.library", 1L)))
  345.         {
  346.             if (pp->pr_CLI && DOSBase)
  347.                 {
  348.                     DosWrite(pp->pr_COS, "You need icon.library V1+ \n", 27L);
  349.                 }
  350.             return;
  351.         }
  352.  
  353.  
  354.     if (WBenchMsg)
  355.         {
  356.             CurrentDir(WBenchMsg->sm_ArgList->wa_Lock);
  357.             _argv = (char **) WBenchMsg;
  358.             _argc = 0;
  359.         }
  360.     else
  361.         {
  362.             /*
  363.              *    WARNING: With that function, argv[] array will not start with the program
  364.              *    name but with it's first argument. If there's no args, argc will be zero.
  365.              */
  366.             _argv = ArpAlloc(4 * 7);
  367.             _argc = (int) GADS(aptr, alen, HelpUsage, _argv, HelpArg);
  368.             if (_argc < 0)
  369.                 {
  370.                     Puts(_argv[0]);
  371.                     exit(20);
  372.                 }
  373.         }
  374.  
  375.  
  376.     main(_argc, _argv);
  377.  
  378.     exit(0);
  379. }
  380.